package service

import (
	"github.com/spf13/viper"
)

// ServerConfig ...
type ServerConfig struct {
	Port int `yaml:"port"`
}

// MysqlConfig ...
type MysqlConfig struct {
	Host     string `yaml:"host"`
	Port     int    `yaml:"port"`
	Username string `yaml:"username"`
	Password string `yaml:"password"`
	Database string `yaml:"database"`
}

// QiniuConfig ...
type QiniuConfig struct {
	Bucket    string `yaml:"bucket"`
	AccessKey string `yaml:"accesskey"`
	SecretKey string `yaml:"secret"`
}

// Configuration ...
type Configuration struct {
	Server ServerConfig `yaml:"server"`
	Mysql  MysqlConfig  `yaml:"mysql"`
	Qiniu  QiniuConfig  `yaml:"qiniu"`
}

var (
	configuration *Configuration
)

// ParseConfiguration ...
func ParseConfiguration(configPath string, configType string) error {
	v := viper.New()
	v.SetConfigFile(configPath)
	// v.SetConfigType(configType)

	if err := v.ReadInConfig(); err != nil {
		return err
	}

	var config Configuration
	if err := v.Unmarshal(&config); err != nil {
		return nil
	}

	configuration = &config
	return nil
}

// GetConfiguration ...
func GetConfiguration() *Configuration {
	return configuration
}
